import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_custompaint_first/Widget/chart/BarChart.dart';
import 'Widget/scale/ScaleScrollController.dart';

import 'Widget/scale/ScaleWidget.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Scale Widget'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  double width;

  double height;

  String value = '0';
  int lineInterval = 10;
  double interval = 0.1;
  ScaleScrollController scrollController;

  @override
  void initState() {
    // TODO: implement initState
    scrollController = new ScaleScrollController(lineInterval, interval);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    Size size = MediaQuery.of(context).size;
    width = size.width - 50;
    height = 200;
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Column(
        children: [
          SizedBox(height: 20,),
          // 刻度尺
          Container(
            child: Stack(
              alignment: Alignment.center,
              children: <Widget>[
                Container(
                  alignment: Alignment.center,
                  width: width,
                  height: height,
                  padding: EdgeInsets.only(left: 10, right: 10),
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.all(Radius.circular(10)),
                    gradient: LinearGradient(
                      begin: Alignment.topCenter,
                      end: Alignment.bottomCenter,
                      colors: [
                        Colors.blue,
                        Color(0xff800080),
                      ],
                    ),
                  ),
                  child: ScaleWidget(
                      width - 20, // 这里减去Container的边距
                      height - 100,
                      controller: scrollController,
                      showTopLine: true,
                      bgColor: Colors.transparent,
                      interval: interval, onChange: (v) {
                    setState(() {
                      value = v;
                    });
                  }),
                ),
                Center(
                  child: Container(
                      margin: EdgeInsets.only(bottom: 130),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Text(
                            "${value}",
                            style: TextStyle(color: Colors.white, fontSize: 25),
                          ),
                          SizedBox(
                            width: 10,
                          ),
                          Text(
                            "Mhz",
                            style: TextStyle(
                              color: Colors.white,
                              fontSize: 14,
                            ),
                          )
                        ],
                      )),
                )
              ],
            ),
          ),
          SizedBox(height: 20,),
          // 柱状图
          // Container(
          //   width: width,
          //   height: 200,
          //   decoration: BoxDecoration(
          //     color: Colors.white,
          //     boxShadow: [BoxShadow(
          //       color: Colors.grey,
          //       offset: Offset(2,2),
          //       blurRadius: 2,
          //       spreadRadius: 2
          //     )]
          //   ),
          //   child: BarChart(),
          // )
        ],
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          scrollController.scrollTo(Random().nextInt(100).toDouble());
          // scrollController.goTo(Random().nextInt(100).toDouble());
        },
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}
